#!/bin/bash
# Project: bash_tools
# Filename: scriptlock.lib.sh
# Author: Mariusz Iwanski
# Date: 2012-07-08
# Version: 1.0
# License: 
#
# Describe: Library contains Script Lock functionality. It prevents one script sunning in more than one instance.
#
# Including: Library. Add vector library to your code using ". vector.lib.sh"
#
# Library postfix:  _889_lock - this is to ensure artificial encapsulation.



#VARIABLES
#full path of script invoking this LIB
#Only lock script that is directly including this LIB.
#BASH_SOURCE[0] - this LIB, LIFO stack
#BASH_SOURCE[1] - script that included this LIB
#BASH_SOURCE[2] - some other script further in execution sequence.
#Interesting part is that it gives different results when assigning here, and in function
#IT HAS TO BE ASSIGNED IN FUNCTION TO RETURN FULL PATH !
#script_889_lock=${BASH_SOURCE[1]}
script_889_lock=0
#Better option to check full path of file is using "readlink" command
script_889_lock=`/bin/readlink -f $0`
#info strings
locked_txt_889_lock="Script lock is enabeled"
#lock flag
flag_889_lock=0
#lock file
file_lock_889_lock="/tmp/889_lock_scriptlock.lib.sh_"$RANDOM"_"$PPID

# PRIVATE FUNCTION
# Name: printHelp_889_lock()
# Describe: Help function for library
# Usage: printHelp_889_lock - no arguments
# Exit codes: -
printHelp_889_lock ()
{
  echo ""
  echo "Library: scriptlock.lib.sh"
  echo "	 Public: "
  echo "		lock_enable() - create lock file. Start blocking multiple script execution."
  echo "		lock_disable() - remove lock file. Stop blocking multiple script execution."
  echo "		lock_info() - display current lock status "
  echo ""
  echo "	 Private: "
  echo "		printHelp_889_lock () - used to print library help."
  echo "			Do not use directly.Use <your_script> -lib scriptlock.lib.sh to see help."
  echo "		check_889_lock () - Checks if lock file exists."
  echo "		clean_889_lock () - Remove lock file on script exit."
  echo "			This function is used by TRAP set on INT TERM EXIT"
  echo ""
  echo "	 Library postfix: _889_lock - this is to ensure artificial encapsulation."
  echo ""
}


if [ $# == 2 ] && [ $1 == "-lib" ] && [ $2 == "scriptlock.lib.sh" ]; then
    printHelp_889_lock 
    exit 0
fi


# FUNCTION
# Name: check_889_lock ()
# Describe: Checks if lock file exists.
# Usage: check_889_lock () - Check if lock file exists.
# Exit codes: - 
check_889_lock ()
{
	#Check if lock file exists in /tmp folder
	#script_889_lock=${BASH_SOURCE[1]}
	for i in `/bin/ls -1 /tmp | grep 889_lock_scriptlock.lib.sh`;
	do 
	  tmp_889_lock=`/bin/cat /tmp/$i`
	  if [ $tmp_889_lock = $script_889_lock ]; then
		  flag_889_lock=1	
	  fi
	done		
}

# FUNCTION
# Name: lock_enable()
# Describe: Create lock file. Start blocking multiple script execution.
# Usage: lock_enable() - no arguments
# Exit codes: -
lock_enable ()
{
	check_889_lock
	if [ $flag_889_lock = 0 ]; then
	#script_889_lock=${BASH_SOURCE[1]}
	echo $script_889_lock > $file_lock_889_lock
	flag_889_lock=1
	else
	echo $locked_txt_889_lock
	exit 1
	fi		
}

# FUNCTION
# Name: clean_889_lock
# Describe: Create lock file. Start blocking multiple script execution.
# Usage: lock_enable() - no arguments
# Exit codes: -
clean_889_lock ()
{
	rm -f $file_lock_889_lock
}

#Trap to automatycaly remove lock file
trap clean_889_lock INT TERM EXIT

#enable locking
lock_enable
